// https://vuex.vuejs.org/zh-cn/intro.html
// make sure to call Vue.use(Vuex) if using a module system
import Vue from 'vue';
import Vuex from 'vuex';
import createPersistedState from 'vuex-persistedstate';

Vue.use(Vuex);

const store = new Vuex.Store({
  state: {
    loading: false,
    searchValue: '', // 搜索框输入值
    searchKey: '', // 列表点击对应的精准关键字
    companyInfo: {}, // 公司所有数据
    company: {}, //公司部分数据
    token: '',
    userInfo: {},
    showLogin: false
  },
  mutations: {
    setLoading(state, payload) {
      state.loading = payload;
    },
    setSearchValue(state, payload) {
      state.searchValue = payload;
    },
    setSearchKey(state, payload) {
      state.searchKey = payload;
    },
    setCompanyInfo(state, payload) {
      // state.companyInfo = payload;
      state.companyInfo = Object.assign({}, payload);
    },
    setCompany(state, payload) {
      // state.companyInfo = payload;
      state.company = Object.assign({}, payload);
    },
    setToken(state, payload) {
      state.token = payload;
    },
    setUserInfo(state, payload) {
      state.userInfo = payload;
    },
    setShowLogin(state, payload) {
      state.showLogin = payload;
    }
  },
  actions: {
    test(context) {
      // context.commit('increment')
      console.log(context);
    }
  },
  getters: {
    loading: state => state.loading,
    searchValue: state => state.searchValue,
    searchKey: state => state.searchKey,
    companyInfo: state => state.companyInfo,
    company: state => state.company,
    token: state => state.token,
    userInfo: state => state.userInfo,
    showLogin: state => state.showLogin
  },
  plugins: [
    createPersistedState({
      storage: {
        getItem: key => wx.getStorageSync(key),
        setItem: (key, value) => wx.setStorageSync(key, value),
        removeItem: key => {} //小程序每次进入都会执行removeItem方法，导致数据存储不下来，所以removeItem写为一个空函数！！
      }
    })
  ]
});

export default store;
